const Koa = require('koa')
const onerror = require('koa-onerror')
require('@babel/register')

const app = new Koa()

onerror(app)

// logger
app.use(async (cxt, next) => {
  await next()
  const rt = cxt.response.get('X-Response-Time')
  console.log('repsonse.method: %s, response.url: %s, response.rt: %s', cxt.method, cxt.url, rt)
})

// x-response-time
app.use(async (cxt, next) => {
  const start = +new Date()
  await next()
  const now = +new Date()
  cxt.set('X-Response-Time', now - start)
})

app.use(async cxt => {
  cxt.body = 'hello world!'
})

app.listen('3000')
